package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.ModuleFields;
import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.ModuleStatus;
import com.e3.template.tgf.TGFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyGridHelper;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
/**
 * 存放Moudle模块的公用算法和公用功能的
 * 帮助类。
 * @author Janty
 *
 */
public class ModuleHelper extends TGFHelper<ModuleModel>{
	
	public static ModuleHelper INSTANCE = new ModuleHelper();
	
	private String[] notNullFieldNames = {ModuleModel.CODE,ModuleModel.NAME};
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		
		boolean isNotNull = isNotNullField(name);
		if(name.equals(ModuleFields.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,10,isNotNull);
		}
		else if(name.equals(ModuleFields.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
		}
		else if(name.equals(ModuleFields.PATH)){
			formField = MyFormFieldFactory.createStringField(name,dispName,100,isNotNull);
		}
		else if(name.equals(ModuleFields.STATUS)){
			formField = MyFormFieldFactory.createListField(name, dispName, getStatusListTypeModel(),true,isNotNull);
		}
		else if(name.equals(ModuleFields.PARENT_MODULE)){
			formField = MyFormFieldFactory.createRefField(name, dispName,new ModuleRefDialog(),isNotNull);
		}
		else if(name.equals(ModuleFields.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}
		return formField;
	}
	
	private List<ListTypeModel<Integer>> getStatusListTypeModel(){
		List<ListTypeModel<Integer>> statusValues = new ArrayList<ListTypeModel<Integer>>();
		Integer code = ModuleStatus.STATUS_NORMAL;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ModuleModel.STATUS_NORMAL_NAME));
		statusValues.add(ltModel);
		code = ModuleStatus.STATUS_FORBID;
		ltModel = new ListTypeModel<Integer>(code,getFieldDisplayName(ModuleModel.STATUS_FORBID_NAME));
		statusValues.add(ltModel);
		return statusValues;
	}

	/**
	 * 设置Moudle间的父子关系。			
	 * @param pModule
	 * @param children
	 */
	public void setModuleRelations(ModuleModel pModule,List<ModuleModel> children){
		//pModule.setSubModules(children);
		for (ModuleModel moduleModel : children) {
			moduleModel.setParentModel(pModule);
		}
	}
	
	public List<ColumnConfig> getModuleGridColumnConfigs(){
			List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
			RowNumberer numberer = new RowNumberer();   
			configs.add(numberer);
			
			ColumnConfig column = new ColumnConfig();
			String name = ModuleModel.CODE;
		    column.setId(name);   
		    column.setHeader(getFieldDisplayName(name));   
		    column.setWidth(200);   
		    configs.add(column);  
		    
		    column = new ColumnConfig();
			name = ModuleModel.NAME;
		    column.setId(name);   
		    column.setHeader(getFieldDisplayName(name));   
		    column.setWidth(200);   
		    configs.add(column);  
		    
		    column = new ColumnConfig();
			name = ModuleModel.PARENT_MODULE;
		    column.setId(name);   
		    column.setHeader(getFieldDisplayName(name));   
		    column.setWidth(200);   
		    configs.add(column);  
		    
		    column = new ColumnConfig();
			name = ModuleModel.PATH;
		    column.setId(name);   
		    column.setHeader(getFieldDisplayName(name));   
		    column.setWidth(200);   
		    configs.add(column);  
		    
		    Object[] statusCodes = {ModuleStatus.STATUS_NORMAL,ModuleStatus.STATUS_FORBID};
		    String st1 = getFieldDisplayName(ModuleModel.STATUS_NORMAL_NAME);
		    String st2 = getFieldDisplayName(ModuleModel.STATUS_FORBID_NAME);
		    String[] statusNames = {st1,st2};
		    GridCellRenderer<ModuleModel> statusCellRenderer = MyGridHelper.createListCellRender(ModuleModel.STATUS, statusCodes, statusNames);
		    column = new ColumnConfig();
			name = ModuleModel.STATUS;
		    column.setId(name);   
		    column.setHeader(getFieldDisplayName(name));   
		    column.setWidth(200); 
		    column.setRenderer(statusCellRenderer);
		    configs.add(column); 
		
		    return configs;

	}
	
	public String getFieldDisplayName(String code) {
		return ModuleFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String getCodeFieldName() {
		return ModuleModel.CODE;
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFieldNames;
	}

	@Override
	public ModuleModel createModelInstance() {
		ModuleModel model = new ModuleModel();
		model.setStatus(ModuleStatus.STATUS_NORMAL);
		return model;
	}
}
